package com.ebupt.migu.music.api.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.ebupt.migu.music.api.entity.StrategyInfoBean;
import com.ebupt.migu.music.api.entity.StrategyInfoEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
@DS("mysql_mg")
public interface MemberPopMapper {
    /**
     *  查询的是 1.查询的是在当前时间在这个执行策略周期的策略信息
     *          2.优先级以分群会员优先，全客户端会员次之
     * @return
     */
    @Select(" select black,white,group_id,vip_strategy_photo,vip_strategy_link  from strategy_info where  strategy_type=7 and execute_status=4   " +
            "and now()  BETWEEN  start_time and   end_time ORDER BY vip_target_crowd,create_time desc ")
    List<StrategyInfoEntity> StrategyInfoList();


    /**
     *  查询的是 取全客户端会员最新有效数据
     *
     * @return
     */
    @Select(" select black,white,group_id,vip_strategy_photo,vip_strategy_link  from strategy_info where  strategy_type=7 and execute_status=4 and vip_target_crowd=1  " +
            " and now()  BETWEEN  start_time and   end_time ORDER BY create_time desc limit 1" )
    StrategyInfoEntity strategyInfo();

    /**
     *   新增策略
     * @param strategyInfo
     * @return
     */
    @Insert("insert into strategy_info(strategy_id,strategy_name,strategy_type,group_id,group_name,creator_id,creator_name,creator_department,creator_phone " +
            " ,creator_email,white,black,create_time,execute_type,execute_status,remarks,start_time,end_time,vip_target_crowd,vip_strategy_photo,vip_strategy_link " +
            " ,approver_id,approver_name,construction_json)"+
            "  values(#{strategyId},#{strategyName},#{strategyType},#{groupId},#{groupName},#{creatorId}" +
            " ,#{creatorName},#{creatorDepartment},#{creatorPhone},#{creatorEmail},#{white} " +
            " ,#{black},now(),#{executeType},#{executeStatus},#{remarks},#{startTime} " +
            " ,#{endTime},#{vipTargetCrowd},#{vipStrategyPhoto},#{vipStrategyLink},#{approverId},#{approverName},#{constructionJson})")
    int   addStrategy(StrategyInfoBean strategyInfo);


    /**
     *   根据类型去查询，返回对应类型的结果集
     * @param map
     * @return
     */
   List<String>  rosterInfoByType(Map<String,Object> map);


    /**
     *  修改弹窗策略
     * @param strategyInfo
     * @return
     */
    @Update({"<script>" +
             " update  strategy_info set "+
             "<if test='strategyName != null and  strategyName != \"\" '>"+
             " and   strategy_name=#{strategyName}, "+
             "</if> "+
            "<if test='groupId != null and  groupId != \"\" '>"+
            " and   group_id=#{groupId}, "+
            "</if> "+
            "<if test='groupName != null and  groupName != \"\" '>"+
            " and   group_name=#{groupName}, "+
            "</if> "+
            "<if test='approverId != null and  approverId != \"\" '>"+
            " and   approver_id=#{approverId}, "+
            "</if> "+
            "<if test='approverName != null and  approverName != \"\" '>"+
            " and   approver_name=#{approverName}, "+
            "</if> "+
            "<if test='white != null and  white != \"\" '>"+
            " and   white=#{white}, "+
            "</if> "+
            "<if test='black != null and  black != \"\" '>"+
            " and   black=#{black}, "+
            "</if> "+
            "<if test='executeStatus != null and  executeStatus != \"\" '>"+
            " and   execute_status=#{executeStatus}, "+
            "</if> "+
            "<if test='executeType != null and  executeType != \"\" '>"+
            " and   execute_type=#{executeType}, "+
            "</if> "+
            "<if test='remarks != null and  remarks != \"\" '>"+
            " and   remarks=#{remarks}, "+
            "</if> "+
            "<if test='remarks != null and  remarks != \"\" '>"+
            " and   start_time=#{remarks}, "+
            "</if> "+
            "<if test='remarks != null and  remarks != \"\" '>"+
            " and   end_time=#{remarks}, "+
            "</if> "+
            "<if test='vipTargetCrowd != null and  vipTargetCrowd != \"\" '>"+
            " and   vip_target_crowd=#{vipTargetCrowd}, "+
            "</if> "+
            "<if test='vipStrategyPhoto != null and  vipStrategyPhoto != \"\" '>"+
            " and   vip_strategy_photo=#{vipStrategyPhoto}, "+
            "</if> "+
            "<if test='vipStrategyLink != null and  vipStrategyLink != \"\" '>"+
            " and   vip_strategy_link=#{vipStrategyLink}, "+
            "</if> "+
             "</script>"})
    int   updateStrategy(StrategyInfoBean strategyInfo);

    /**
     *  判断修改人是否是创建人
     * @return
     */
    @Select("select count(id)  from strategy_info where creator_id=#{creatorId} and strategy_id=#{strategyId}")
    int  IsAlike(@Param("creatorId") String creatorId,@Param("strategyId") String strategyId);
}
